//this allows the user to "show all" or "browse by name"
package edu.unc.comp410.colemangeer.ui;
//TODO: make the log.warns for all of my classes

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;
import org.omg.CORBA.portable.ApplicationException;

import edu.unc.comp410.colemangeer.data.*;
import edu.unc.comp410.colemangeer.demos.CatalogDemo;
import edu.unc.comp410.colemangeer.demos.ProductsDemo;
import edu.unc.comp410.colemangeer.model.Catalog;
import edu.unc.comp410.colemangeer.model.CatalogBuilder;
import edu.unc.comp410.colemangeer.model.DataProvider;
import edu.unc.comp410.colemangeer.system.UIController;

public class BrowseGeneralGUI {
	
	//declarations
	public static final boolean USE_EMULATOR = true;
	private static Logger log = Logger.getLogger(BrowseGeneralGUI.class);
	private UIController controller = new UIController();

	// Text fields and their labels.
	private final String SPACING = "  ";
	private final JLabel NameLabel = new JLabel( "Product Name Search" + SPACING, SwingConstants.RIGHT );
	private final JTextField name = new JTextField( 25 );
	
	// Buttons and their event handlers.
	private final static JButton browseByNameButton = new JButton( "Browse By Name" );
	private final browseByNameHandler browseByNameHandler = new browseByNameHandler();
	private final static JButton browseAllButton = new JButton( "Browse All Products" );
	private final browseAllHandler browseAllHandler = new browseAllHandler();
	
	/**
	 * Thread-safe execution of this GUI. You don't have to understand
	 * this. Just us it as-is for all your Swing programs.
	 */
	public static void main( String[] args ) {
		System.out.println( "Main called" );
		//call instatiateCatalog method here
		instantiateCatalog();
		javax.swing.SwingUtilities.invokeLater( new Runnable() {
			public void run() { createAndShowGUI(); }
		});
		System.out.println( "Main finished" );
	}
	
	//builds the catalog and the cache2
	public static void instantiateCatalog(){ 
		DataProvider dataProvider = new DataProvider();
		CatalogBuilder catalogBuilder = null;
		try {
			catalogBuilder = new CatalogBuilder( dataProvider.getProductGateway() );
		} catch (ApplicationException e) {
			log.error("data problem: could not get IProductGateway");
		}
		Catalog allCatalogItems = catalogBuilder.getCatalog();
		
		log.info("Items in catalog...");
		allCatalogItems.logCatalog();
	}
	
	/** browse all
	 * Define the action performed when "Clear" button is clicked.
	 * Note - anonymous inner class often used here.
	 */
	private class browseAllHandler implements ActionListener {
		public void actionPerformed( ActionEvent e ) {
			DataChooser2.searchAll();
		}
	} // End browseAllHandler class.
	
	
	
	/** browse by name
	 * Define the action performed when "Enter" button is clicked.
	 */
	private class browseByNameHandler implements ActionListener  {
		public void actionPerformed( ActionEvent e )  {
			
			String nameValue = name.getText();
			
			if(nameValue.length() == 0){
				//TODO: write error handling here
				System.out.println("you must enter something");
			}
			else{ 
				try{
					DataChooser2.searchByName( nameValue );
				}
				catch (ClassNotFoundException x) {
					x.printStackTrace();
				}
			} //end if namevalue.length == 0
				
		}
	} // End browseByNameHandler class.
	
	/**
	 * Create a panel that is the contents of the window except for
	 * its frame (window title plus buttons to minimize, maximize,
	 * and close the window).
	 */
	public Component createComponents() {
	
		JPanel pane = new JPanel( new GridLayout(8,2));
	
		// Place text fields.
		pane.add( NameLabel );
		pane.add( name );
	
		// Place buttons.
		pane.add( new JLabel( "" ) );
		browseByNameButton.addActionListener( browseByNameHandler );
		pane.add(browseByNameButton);
		pane.add( new JLabel( "" ) );
		browseAllButton.addActionListener( browseAllHandler );
		pane.add( browseAllButton );
		
		pane.setBorder( BorderFactory.createEmptyBorder(10,10,10,10));
		return pane;
	}
	
	/**
	 * Creates a single frame with standard decorations
	 * (title + buttons to minimize, maximize, and close)
	 * and contents created by createComponents().
	 */
	private static void createAndShowGUI() {
		JFrame.setDefaultLookAndFeelDecorated( true );
		JFrame frame = new JFrame( "Choose a Browse method");
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.getRootPane().setDefaultButton( browseByNameButton );
		BrowseGeneralGUI app = new BrowseGeneralGUI();
		Component contents = app.createComponents();
		frame.getContentPane().add( contents, BorderLayout.CENTER );
		frame.pack();
		frame.setVisible( true );
	}
	
}
